'use strict'

/**
 * 路由器
 */
//------------------------------------------------------------------
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
//------------------------------------------------------------------

/**
 * 路由器实例
 */
let $router = null

/**
 * 编辑路由器列表
 */
const $getRoutePath = (options = {}) => {
    let defaultTemplateChildren = [
        ...options.routers,
        {
            name: "404",
            path: '/:pathMatch(.*)*',
            component: () => options.$error404
        }
    ]

    return [
        {
            path: '/',
            name: 'defaultTemplate',
            meta: {},
            component: () => options.$motherboard,
            children: defaultTemplateChildren
        }
    ]
}

/**
 * 系统路由器
 */
export default class Router {

    /**
     * 构建路由器
     *
     * @param options 构建配置
     */
    constructor(options) {
        let routes = $getRoutePath(options)
        $router = createRouter({
            history: options.historyMode === 'hash'? createWebHashHistory(options.base): createWebHistory(options.base),
            base: '',
            routes
        })
    }
    /**
     * 获取路由实例
     */
    instance() {
        return $router
    }
}
